﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.ComponentModel;

namespace JapaneseMusicHtmlCreator
{
    /// <summary>
    /// PageSettings.xaml 的交互逻辑
    /// </summary>
    public partial class PageSettings : Page
    {
        private MainWindow mainWindow;
        public WriteableBitmap bitmapOri = null;
        public WriteableBitmap bitmap=null;
        public WriteableBitmap bitmapOri2 = null;
        public WriteableBitmap bitmap2 = null;
        public bool bBlack = false;
        public bool bSlide = false;

       
        public PageSettings(MainWindow mainWindow)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.mainWindow = mainWindow;
            System.Windows.Data.Binding binding = new System.Windows.Data.Binding();
            binding.Source = sliderOpa;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.Default;
            binding.StringFormat = "{0:N0}";
            lbOpa.SetBinding(System.Windows.Controls.TextBlock.TextProperty, binding);
        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            SaveMemory();
            mainWindow.NavigateForward(this, mainWindow.pageCreateAndSave);
          
            
        }

        

        private void btnPrev_Click(object sender, RoutedEventArgs e)
        {
            SaveMemory();
            mainWindow.mediaPlayer.MediaOpened+=mediaPlayer_MediaOpened;
            mainWindow.mediaPlayer.Open(new Uri(mainWindow.musicPath, UriKind.Absolute));
            
        }

        void mediaPlayer_MediaOpened(object sender, EventArgs e)
        {
            mainWindow.mediaPlayer.MediaOpened -= mediaPlayer_MediaOpened;
            mainWindow.NavigateBackward(this, mainWindow.pageKashiEdit);
        }

        private void SaveMemory()
        {
         
            mainWindow.musicBkgndUseBlack = bBlack;
            mainWindow.musicBkgndValue = sliderOpa.Value;
            mainWindow.musicKashiColor = (btnKashiColor.Background as SolidColorBrush).Color;
            mainWindow.musicCurrentKashiColor = (btnCurrentKashiColor.Background as SolidColorBrush).Color;
            mainWindow.musicKanaColor = (btnKanaColor.Background as SolidColorBrush).Color;
            mainWindow.musicTranslateColor = (btnTranslateColor.Background as SolidColorBrush).Color;
            mainWindow.musicTitleColor = (btnTitleColor.Background as SolidColorBrush).Color;
            mainWindow.musicArtistColor = (btnArtistColor.Background as SolidColorBrush).Color;
            mainWindow.musicBackgroundColor=(btnBackgroundColor.Background as SolidColorBrush).Color;
            double.TryParse(tbAnimeTime.Text, out mainWindow.musicLineAnimeTime);
            double.TryParse(tbLineFontSize.Text, out mainWindow.musicLineFontSize);
            double.TryParse(tbCurrentLineFontSize.Text, out mainWindow.musicCurrentLineFontSize);
            double.TryParse(tbTitleFontSize.Text, out mainWindow.musicTitleFontSize);
            double.TryParse(tbArtistFontSize.Text, out mainWindow.musicArtistFontSize);
          
        }

        private void btnAddPic_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "背景图片文件|*.jpg;*png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ofd.FileName))
                {
                    mainWindow.musicBkgndPath = ofd.FileName;
                    bitmapOri = new WriteableBitmap(new BitmapImage(new Uri(mainWindow.musicBkgndPath, UriKind.Absolute)));
                    bitmap = new WriteableBitmap(new BitmapImage(new Uri(mainWindow.musicBkgndPath, UriKind.Absolute)));
                    FreshImg();
                    
                }
            }
        }

        private void sliderOpa_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            FreshImg();
        }

        public void FreshImg()
        {
            if (mainWindow!=null&&mainWindow.musicBkgndPath != null&&bitmap!=null)
            {
                Int32Rect rect = new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight);
                int stride = bitmap.Format.BitsPerPixel * rect.Width / 8;
                byte[] data = new byte[rect.Height * stride];
                bitmapOri.CopyPixels(rect, data, stride, 0);
                for (int i = 0; i < rect.Height * rect.Width; i++)
                {
                    if (bBlack)
                    {
                        byte b = data[i * 4];
                        data[i * 4] = (byte)(b * (100 - sliderOpa.Value) / 100);
                        b = data[i * 4 + 1];
                        data[i * 4 + 1] = (byte)(b * (100 - sliderOpa.Value) / 100);
                        b = data[i * 4 + 2];
                        data[i * 4 + 2] = (byte)(b * (100 - sliderOpa.Value) / 100);
                    }
                    else
                    {
                        byte b = data[i * 4];
                        data[i * 4] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                        b = data[i * 4 + 1];
                        data[i * 4+1] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                        b = data[i * 4 + 2];
                        data[i * 4+2] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                    }

                }
                bitmap.WritePixels(rect, data, stride, 0);
                imgBkgnd.Source = bitmap;
            }
            if (mainWindow != null && mainWindow.musicBkgndPath2 != null && bitmap2 != null)
            {
                Int32Rect rect = new Int32Rect(0, 0, bitmap2.PixelWidth, bitmap2.PixelHeight);
                int stride = bitmap2.Format.BitsPerPixel * rect.Width / 8;
                byte[] data = new byte[rect.Height * stride];
                bitmapOri2.CopyPixels(rect, data, stride, 0);
                for (int i = 0; i < rect.Height * rect.Width; i++)
                {
                    if (bBlack)
                    {
                        byte b = data[i * 4];
                        data[i * 4] = (byte)(b * (100 - sliderOpa.Value) / 100);
                        b = data[i * 4 + 1];
                        data[i * 4 + 1] = (byte)(b * (100 - sliderOpa.Value) / 100);
                        b = data[i * 4 + 2];
                        data[i * 4 + 2] = (byte)(b * (100 - sliderOpa.Value) / 100);
                    }
                    else
                    {
                        byte b = data[i * 4];
                        data[i * 4] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                        b = data[i * 4 + 1];
                        data[i * 4 + 1] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                        b = data[i * 4 + 2];
                        data[i * 4 + 2] = (byte)((255 - b) * sliderOpa.Value / 100 + b);
                    }

                }
                bitmap2.WritePixels(rect, data, stride, 0);
                imgBkgnd2.Source = bitmap2;
            }
        }

        private void rbWhite_Checked(object sender, RoutedEventArgs e)
        {
            bBlack = false;
            FreshImg();
        }

        private void rbBlack_Checked(object sender, RoutedEventArgs e)
        {
            bBlack = true;
            FreshImg();
        }

        private void sliderOpa_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            bSlide = true;
        }

        private void sliderOpa_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (bSlide)
            {
                bSlide = false;
                FreshImg();
            }
        }

        private void sliderOpa_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            bSlide = true;
        }

        private void sliderOpa_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (bSlide)
            {
                bSlide = false;
                FreshImg();
            }
        }

     
        

        private void btnKashiColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnKashiColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnCurrentKashiColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnCurrentKashiColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

     

        private void btnKanaColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnKanaColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnTranslateColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnTranslateColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnTitleColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnTitleColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnArtistColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnArtistColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnBackgroundColor_Click(object sender, RoutedEventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                System.Drawing.Color c = dialog.Color;
                btnBackgroundColor.Background = new SolidColorBrush(Color.FromRgb(c.R, c.G, c.B));
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            imgBkgnd.Source = null;
            bitmap = null;
            bitmapOri = null;
            mainWindow.musicBkgndPath = "delete";
        }

        private void btnDelete2_Click(object sender, RoutedEventArgs e)
        {
            imgBkgnd2.Source = null;
            bitmap2 = null;
            bitmapOri2 = null;
            mainWindow.musicBkgndPath2 = "delete";
        }

        private void btnAddPic2_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Environment.CurrentDirectory;
            ofd.Filter = "背景图片文件|*.jpg;*png";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (!string.IsNullOrEmpty(ofd.FileName))
                {
                    mainWindow.musicBkgndPath2 = ofd.FileName;
                    bitmapOri2 = new WriteableBitmap(new BitmapImage(new Uri(mainWindow.musicBkgndPath2, UriKind.Absolute)));
                    bitmap2 = new WriteableBitmap(new BitmapImage(new Uri(mainWindow.musicBkgndPath2, UriKind.Absolute)));
                    FreshImg();

                }
            }
        }
    }
}
